import {
  user_info, role_info , setSession,getSession
} from '../../utils/config'
import { setToken } from '../../utils/auth'
const user = {
  state: {
    user: {
      name: '',
      avatar: '',
      gender: 0,
      weather:{}
    },
    roles: [],
  },
  mutations: {
    SET_USER: (state, user) => {
      let u = getSession(user_info)
      if (user.token) {
        setToken(user.token)
      }
      state.user = Object.assign({},u,user)
      setSession(user_info, state.user)
    },
    SET_ROLES: (state,roles) => {
      let r = state.roles
      state.roles = Object.assign(r, roles)
      setSession(role_info, state.roles)
    },
    REMOVE_USER: (state) => {
      state.user = {
        name: '',
        avatar: '//img.mukewang.com//54584d1d00015c0b02200220.jpg',
        gender: 0,
        weather:{}
      }
      state.roles = []
      setSession(user_info, state.user)
      setSession(role_info, state.roles)
    }
  }
}

export default user
